<template>
  <!-- 新增/修改 -->
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="small"
    >
      <ChargeStrategy
        v-if="visible"
        ref="chargeStrategy"
        :chargeForm="chargeForm"
      ></ChargeStrategy>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import ChargeStrategy from "@/components/ChargeStrategy";
import { setGlobalChargeStrategy } from "@/api/charge/site";
export default {
  components: { ChargeStrategy },
  props: {
    title: {
      type: String,
      require: true,
    },
    visible: {
      type: Boolean,
      require: true,
    },
  },
  data() {
    return {
      // 表单参数
      form: {},
      region: [],
      regionOptions: [
        {
          value: "江苏省",
          label: "江苏省",
          children: [
            {
              value: "宿迁市",
              label: "宿迁市",
              children: [
                {
                  value: "市辖区",
                  label: "市辖区",
                  children: [
                    {
                      value: "宿城区",
                      label: "宿城区",
                    },
                    {
                      value: "宿豫区",
                      label: "宿豫区",
                    },
                  ],
                },
                {
                  value: "县",
                  label: "县",
                  children: [
                    {
                      value: "沭阳县",
                      label: "沭阳县",
                    },
                    {
                      value: "泗阳县",
                      label: "泗阳县",
                    },
                    {
                      value: "泗洪县",
                      label: "泗洪县",
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
      // 表单校验
      rules: {},
      chargeForm: {},
    };
  },
  watch: {
    visible(val) {
      // 数据回填
      if (val) {
        this.form = {};
        this.region = this.form.region ? this.form.region.split("/") : [];
        this.chargeForm = {};
      }
    },
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = this.getFormParams();
          setGlobalChargeStrategy(params).then((res) => {
            this.$modal.msgSuccess("全局配置成功");
            this.$refs["form"].resetFields();
            this.$emit("success");
          });
        }
      });
    },
    // 关闭
    cancel() {
      this.$refs["form"].resetFields();
      this.$emit("cancel");
    },
    // 获取 新增/修改 表单参数
    getFormParams() {
      let chargeStrategy = this.$refs.chargeStrategy.getChargeFormValues();
      let params = {
        chargeStrategy: JSON.stringify(chargeStrategy),
      };

      return params;
    },
  },
};
</script>
<style scoped></style>
